<script>
	function arrotonda (num, dec, mod) {
		var div = Math.pow(10, dec);
	 	switch (mod) {
	  		case 'e':
	  		case 'E': //arrotonda per eccesso
	   			return Math.ceil(num * div) / div;
	  		break;
	  		case 'd':
	  		case 'D': //arrotonda per difetto
	   			return Math.floor(num * div) / div;
	  		break;
	  		case 't':
	  		case 'T': //troncamento
	   			return parseInt(num * div) / div;
	  		break;
	  		case 'a':
	  		case 'A': 
	  		default: //arrotonda
	   			return Math.round(num * div) / div;
	 	}
	}

	function setFields(){
		// Riempo i label
		document.getElementById('importoPagamentoSpan').innerHTML = document.getElementById('importoCrediti').value + ' CHF';
		document.getElementById('importoPagamento').value = document.getElementById('importoCrediti').value;
		document.getElementById('importoTassaSpan').innerHTML = arrotonda((document.getElementById('importoCrediti').value/100)*2, 2, 'e') + ' CHF';
		document.getElementById('importoTassa').value = arrotonda((document.getElementById('importoPagamento').value/100)*2, 2, 'e') + ' CHF';
		document.getElementById('importoTotalePagamentoSpan').innerHTML = parseFloat(document.getElementById('importoCrediti').value) + parseFloat(arrotonda((document.getElementById('importoCrediti').value/100)*2, 2, 'e')) + ' CHF';	
		document.getElementById('importoTotalePagamento').value = parseFloat(document.getElementById('importoCrediti').value) + parseFloat(arrotonda((document.getElementById('importoCrediti').value/100)*2, 2, 'e')) + ' CHF';	

		checkValue();
	}

	function checkValue(){
		if(document.getElementById('importoCrediti').value > 1500 || document.getElementById('importoCrediti').value < 15){
			document.getElementById('minMax').style.color = 'red';
			document.getElementById('purchase').disabled = true;
		}else{
			document.getElementById('purchase').disabled = false;
			document.getElementById('minMax').style.color = 'white';
		}
	}
</script>

<div id="containercenter">
	<?php 
		require('navigation.php');
		require('navHistory.php'); 
		$offers = new Offers();
	?>
	<div id="containerleft">
		<h2>Acquista crediti</h2> 
		<form method="POST" name="purchaseCredits" action="index.php?p=checkout">
			<table>
				<tr>
					<td class="paypalImage">
						&nbsp;
					</td>
				</tr>
				<tr style="border-top: 1px dotted #CCC;">
					<td>
						&nbsp;
					</td>
				</tr>
				<tr>
					<td colspan="3" class="paymentBorder">
						<b>Inserisci il numero di crediti desiderato (senza decimali)</b>
						<input type="hidden" name="nomeAcquisto" id="nomeAcquisto" value="Acquisto crediti"></input>
					</td>
				</tr>
				<tr>
					<td class="paymentBorder">
						Importo crediti
					</td>
					<td class="paymentBorderRight">
						<input type="text" id="importoCrediti" name="importoCrediti" onKeyUp="setFields();"></input>
					</td>
					<td class="paymentBorder" id="minMax">
						&nbsp; (Min: 15 crediti, Max: 1500 crediti)   	
					</td>
				</tr>
				<tr>
					<td class="paymentBorder">
						Importo di pagamento
					</td>
					<td class="paymentBorderRight">
						<span id="importoPagamentoSpan"></span>
						<input type="hidden" name="importoPagamento" id="importoPagamento"></input>
					</td>
				</tr>
				<tr>
					<td class="paymentBorder">
						2% di tassa versamento
					</td>
					<td class="paymentBorderRight">
						<span id="importoTassaSpan"></span>
						<input type="hidden" name="importoTassa" id="importoTassa"></input>
					</td>
				</tr>
				<tr>
					<td class="paymentBorder">
						Importo totale di pagamento
					</td>
					<td class="paymentBorderRight">
						<span style="font-weight: bold;" id="importoTotalePagamentoSpan"></span>
						<input type="hidden" name="importoTotalePagamento" id="importoTotalePagamento"></input>
					</td>
				</tr>
				<tr>
					<td colspan="3" style="border-bottom: 1px dotted #999999;">
						&nbsp;
					</td>
				</tr>
				<tr>
					<td>
						<input style="padding-top: 5px;padding-bottom: 5px;" id="purchase" type="image" name="checkout" src="static/images/layout/acquista_ora.png"></input>
					</td>
				</tr>
				<tr>
					<td colspan="3" style="border-top: 1px dotted #999999;">
						&nbsp;
					</td>
				</tr>
			</table
		</form>
		<table>
			<tr>
				<?php 
					$offerte = $offers->getOffers(); 
					if(!$offerte){
						echo 'Spiacenti, al momento non ci sono offerte.';
					}else{
						while($row = $offerte->FetchRow()){
							if($offers->checkOffer($row['ID_OFFER'])){
				?>
					<td width="auto">
						<form method="POST" name="offerBox_<?php echo $row['ID_OFFER'];?>" id="offerBox_<?php echo $row['ID_OFFER'];?>" action="index.php?p=checkout">
							<div id="_wrongFlashVer">
								<div class="bestseller collapsableComponent">
							       	<div class="headbar">
										<div class="controlHeaderNoShadow">
											<span class="spanLeft">
												<?php echo $row['name'];?>
												<input type="hidden" name="nameOffer_<?php echo $row['ID_OFFER'];?>" id="nameOffer_<?php echo $row['ID_OFFER'];?>" value="<?php echo $row['name'];?>"></input>
												<input type="hidden" name="idOffer" id="idOffer" value="<?php echo $row['ID_OFFER'];?>"></input>
											
											</span>
										</div>
										<div class="controlHeaderBottomShadowDropDown">
										</div>
										
							            <div class="headline ControlContent">
							            	<?php echo $row['description'];?>
							            	<input type="hidden" name="descriptionOffer_<?php echo $row['ID_OFFER'];?>" id="descriptionOffer_<?php echo $row['ID_OFFER'];?>" value="<?php echo $row['description'];?>"></input>
							            </div>
							       	</div>
							        <div class="bsitem ControlContent">
					                    <div class="label">
					                        <nobr>Con <?php echo $row['price'];?>.- CHF</nobr>
					                        <input type="hidden" name="priceOffer_<?php echo $row['ID_OFFER'];?>" id="priceOffer_<?php echo $row['ID_OFFER'];?>" value="<?php echo $row['price'];?>"></input>
					                    </div>
					                    <div class="content">
										<div class="bestSellerMain" title="Con questo pacchetto hai la possibilit&agrave; di acquistare 85 crediti con soli 50.- CHF." id="BetSeller">
										    <div class="LeftBetseller" id="LeftBorderBetSeller" onclick="document.offerBox_<?php echo $row['ID_OFFER'];?>.submit()"></div>
										    <div class="MidBetseller" id="betse" onclick="document.offerBox_<?php echo $row['ID_OFFER'];?>.submit();" >Compri <?php echo $row['credits'];?> crediti</div>
										    <input type="hidden" name="creditsOffer_<?php echo $row['ID_OFFER'];?>" id="creditsOffer_<?php echo $row['ID_OFFER'];?>" value="<?php echo $row['credits'];?>"></input>
										    <div class="RightBetseller" id="RightBorderBetSeller" onclick="document.offerBox_<?php echo $row['ID_OFFER'];?>.submit();" ></div>
										</div>
										<input id="offer_<?php echo $row['ID_OFFER'];?>" type="submit" name="checkoutOffer" style="display: none;"></input>
					             	</div>
				                    <div style="font-size: 1px;">
				                        &nbsp;
				                    </div>
					        	</div>
				   			</div>
				   		</form>
					</td>
				<?php }}} ?>
			</tr>
		</table>
	
	</div>
	
	<div id="containerright">
		<h2>Offerte speciali</h2>
	</div>
</div>